// Help
//
// 保佑没Bug
//
// Created by AS on 2019/4/10.
//
// Copyright © 2018 AS. All rights reserved.
//

#import "CommunityController.h"
#import "ItemTableViewCell.h"
#import "UserInfo.h"
#import "PostViewController.h"
#import "CommunityDetailController.h"

@interface CommunityController ()<postViewControllerDeletage>
/**  */
@property (nonatomic,strong) NSMutableArray *dataSource;
/**  */
@property (nonatomic,assign) NSInteger page;
/**  */
@property (nonatomic,assign) NSInteger size;
/**  */
@property (nonatomic,strong) UserInfo *user;

@end


@implementation CommunityController

-(UserInfo *)user{
    if(!_user){
        _user = [UserInfo getUserInfo];
    }
    return _user;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.title;
    self.type = self.type;
    self.view.backgroundColor = HexRGB(0xf3f5f5);
    self.navigationItem.title = self.title;
    self.page = 1;
    self.size = 20;
    
    
    if(!self.type){
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
        [button setTitle:@"发表" forState:UIControlStateNormal];
        button.titleLabel.font = Font16;
        [button addTarget:self action:@selector(postContent) forControlEvents:UIControlEventTouchUpInside];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
        UIBarButtonItem *postItem = [[UIBarButtonItem alloc]initWithCustomView:button];
        self.navigationItem.rightBarButtonItem = postItem;
        
    }
    
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(down_refreshData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];
    
}

-(void)postContent{
    
    PostViewController *view = [PostViewController new];
    view.delegate = self;
    [self pushViewController:view];
}

-(void)down_refreshData{
    self.page = 1;
    [self getData:YES];
}

-(void)loadMoreData{
    self.page ++;
    [self getData:NO];
}

-(void)postViewControllerPostFinsh{
//    [self down_refreshData];
    [self.tableView.mj_header beginRefreshing];
}

-(void)getData:(Boolean)isRefresh{
    
    NSString *url = @"/api/communicationcircle/getCommunicationList";
    
    if ([self.type isEqualToString:@"POST"]) {
        url = @"/api/communicationcircle/mycomminucate";
    }else if ([self.type isEqualToString:@"PRAISE"]){
        url = @"/api/communicationcircle/mydz";
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"uid"] = @(self.user.uid);
    dict[@"loginToken"] = self.user.loginToken;
    dict[@"page"] = @(self.page);
    dict[@"size"] = @(self.size);
    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        if (!error) {
            if (!msg) {
                NSArray *array = [CModel mj_objectArrayWithKeyValuesArray:result];
                if (array.count < self.size) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer resetNoMoreData];
                }
                if (isRefresh) {
                    [self.dataSource removeAllObjects];
                }
                for (CModel *model in array) {
                    CModelFrame *mFrame = [CModelFrame new];
                    mFrame.model = model;
                    [self.dataSource addObject:mFrame];
                }
                [self.tableView reloadData];
            }
        }
    }];
    
    
}



#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[ItemTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.modelF = self.dataSource[indexPath.row];
    cell.type = self.type;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CommunityDetailController *detail = [CommunityDetailController new];
    detail.modelF = self.dataSource[indexPath.row];
    detail.type = self.type;
    [self pushViewController:detail];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CModelFrame *frame = self.dataSource[indexPath.row];
    
    return frame.cellHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


@end
